home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11617 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.3 KB

  1. Path: news.sunquest.com!kitk!kitk
  2. From: kitk@mudshark.sunquest.com (Kit Kauffmann)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Please Help Dos Env.
  5. Date: Thu, 14 Mar 1996 16:22:14
  6. Organization: Sunquest
  7. Message-ID: <kitk.2134.00105F67@mudshark.sunquest.com>
  8. References: <Do2AAK.9KC@relay.sheridanc.on.ca>
  9. NNTP-Posting-Host: kkauffma.sunquest.com
  10. X-Newsreader: Trumpet for Windows [Version 1.0 Rev A]
  11.  
  12. In article <Do2AAK.9KC@relay.sheridanc.on.ca> jason.gerrity@sheridanc.on.ca (Jason Gerrity) writes:
  13. >From: jason.gerrity@sheridanc.on.ca (Jason Gerrity)
  14. >Subject: Please Help Dos Env.
  15. >Date: Sun, 10 Mar 1996 17:08:43 GMT
  16.  
  17. >Can someone please show me how to get a c++
  18. >program to set a DOS environment variable that
  19. >will stay resident in DOS after the c++ program
  20. >has terminated...?
  21.  
  22. >both system and putenv only stay active for the duration
  23. >of the spawned process...
  24.  
  25. >Please, help...
  26.  
  27. >Jason
  28.  
  29. >Jason.gerrity@sheridanc.on.ca
  30.  
  31. Dr. Dobb's published an undocumented DOS call which allowed just this sort of 
  32. thing (this is not C or C++, but DOS interrupts).  Also, this is vintage '86 
  33. or something, and probably doesn't work anymore, but I coded some MS assembler 
  34. to do just this very thing at the time (providing a C interface to this rather
  35. difficult routine).  Whether it still works with the latest DOS, who knows ;-)
  36.  
  37. Anyway, here's the code - have fun:
  38.  
  39. ; This routine uses an undocumented DOS interrupt (2eh) to execute a DOS
  40. ; command of any sort, including "internal" commands such as 'TYPE' or 'DIR'
  41. ; and .COM, .EXE, and .BAT files found on the PATH.  The one drawback of
  42. ; using this function is that all non-static memory must be released before
  43. ; the program is used, using DOS interrupt 4A or some library function such
  44. ; as rstmem from Lattice.  For the ramifications of this detail, see the
  45. ; C routine which is meant to call this routine: 'execute.c'.
  46. ; This routine has been adapted from routines published in the Dec. '86
  47. ; Dr. Dobb's by Kit Kauffmann to be compiled with MASM (I used 4.0) and
  48. ; called from Lattice C programs.
  49.  
  50.      INCLUDE DOS.MAC     ; this is a set of lattice macros for setting the
  51.                          ; memory model dependent information.
  52.  
  53.      SETX                ; determines offset of parameters
  54.  
  55.      PSEG                ; start code area
  56.  
  57.      BEGIN INT2E         ; establish legal function entry point
  58.  
  59.      PUSH BP             ; save registers
  60.      MOV  BP,SP          ; establish new stack frame
  61.      PUSH ES             ; needed by lattice
  62.      PUSH DS             ; ditto
  63.      PUSH SI             ; we point ds:si at command string
  64.      PUSH DX             ; although I did not had a problem with this
  65.      MOV  DL,1           ; during the alteration and debugging of these
  66.                          ; routines, not to mention learning the intricacies
  67.                          ; of stack frame manipulation, int2e is advertised
  68.                          ; to set disk write verification with dl set to 0.
  69.                          ; Admittedly, the chances are small (perhaps less
  70.                          ; than 1 in 256?), but I am an ardent believer in
  71.                          ;  only one law of the universe: Murphy's Law.
  72.  
  73.      MOV  WORD PTR CS:SAVE_SS,SS  ; I know this is dirty programming, but
  74.      MOV  WORD PTR CS:SAVE_SP,SP  ; seems to be no reasonable alternative
  75.                                   ; because both SS & SP must be preserved
  76.                                   ; as must DS and BP, which are are chewed
  77.                                   ; up by this odd interrupt.  For extra
  78.                                   ; data on this trick, read the warning on
  79.                                   ; page 317 of Norton's Programmer's Guide,
  80.                                   ; in which the EXEC interrupt does similar
  81.                                   ; things.
  82.  
  83.      MOV  SI,[BP + X]    ; ds already points at static data area in Lattice
  84.      INT  2EH            ; undocumented interrupt - see DDS 12/86
  85.  
  86.      MOV SS,WORD PTR CS:SAVE_SS   ; restore the cleverly hidden stack data
  87.      MOV SP,WORD PTR CS:SAVE_SP
  88.  
  89.      POP DX              ; restore registers
  90.      POP SI
  91.      POP DS
  92.      POP ES
  93.      POP BP
  94.      JMP RTN             ; jump over hiding place for data
  95.  
  96. SAVE_SS:DW 0             ; storage space (sneaky, huh!)
  97. SAVE_SP:DW 0
  98.  
  99. RTN:
  100.      RET
  101.  INT2E ENDP
  102. ENDPS
  103. END
  104.  
  105. HTH!
  106.  
  107.  
  108. Kit Kauffmann - kitk@mudshark.sunquest.com
  109. AKA 73363,447 (Compu$erve)
  110.  
  111. Finger me for my public key
  112.